<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection" height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)"
            v-if="permission.includes('sys:box-yun-course:add')">添加
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch"
            v-if="permission.includes('sys:box-yun-course:delete')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:box-yun-course:edit')">修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此消息吗？" @confirm="remove(row)">
            <el-link type="danger" slot="reference" :underline="false" icon="el-icon-delete"
              v-if="permission.includes('sys:box-yun-course:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch v-model="row.status" @change="editStatus(row)" :active-value="1" :inactive-value="2" />
        </template>
        
         <!-- 首页有html标签的过滤一下 -->
        <template slot="answer" slot-scope="{row}">
          <div>{{filteredText(row.answer)}}</div>
        </template>

      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import edit from './edit';

export default {
  name: 'YunCourse',
  components: { edit },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/BoxYunCourse/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          minWidth: 45,
          align: 'center',
        },
        {
          prop: 'id',
          label: 'ID',
          minWidth: 60,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'issue',
          label: '疑问',
          minWidth: 120,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'answer',
          label: '解答疑问',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120,
          slot: "answer"
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 80,
          slot: "status"
        },
        {
          prop: 'sort',
          label: '排序',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '添加时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
        },
        {
          columnKey: 'action',
          label: '操作',
          minWidth: 120,
          align: 'center',
          resizable: false,
          slot: 'action',
          // fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },

  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.post('/BoxYunCourse/delete', { id: row.id }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http.post('/BoxYunCourse/delete', { id: this.selection.map(d => d.id) }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },


    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        "id": row.id,
        "status": row.status,
        "is_time": true
      })
      this.$http.post('/BoxYunCourse/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({ type: 'success', message: res.data.msg });
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },

    //过滤掉htm
    filteredText(rawText) {
      const textWithHtml = rawText; // 假设这是包含HTML标签的原始文本
      const regex = /<[^>]+>/g; // 正则表达式匹配HTML标签
      return textWithHtml.replace(regex, '');
    }


  }
}
</script>

<style scoped></style>
